package com.example.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.example.common.Constants;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.ZyzStudent;
import com.example.exception.CustomException;
import com.example.mapper.ZyzStudentMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 志愿者学生业务层
 */
@Service
public class ZyzStudentService {
    @Autowired
    private ZyzStudentMapper zyzStudentMapper;

    /**
     * 添加
     * @param zyzStudent
     */
    public void add(ZyzStudent zyzStudent) {
        ZyzStudent dbZyzStudent = zyzStudentMapper.selectByUsername(zyzStudent.getUsername());
        if (ObjectUtil.isNotNull(dbZyzStudent)){
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        if (ObjectUtil.isEmpty(zyzStudent.getPassword())){
            zyzStudent.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if (ObjectUtil.isEmpty(zyzStudent.getName())){
            zyzStudent.setName(zyzStudent.getUsername());
        }
        String password = zyzStudent.getPassword();
        String md5 = SecureUtil.md5(password);
        zyzStudent.setPassword(md5);
        zyzStudentMapper.insert(zyzStudent);

    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    public int deleteBatch(String ids) {
        return zyzStudentMapper.deleteBatch(Convert.toStrArray(ids));
    }

    /**
     * 修改
     * @param zyzStudent
     */
    public void updateById(ZyzStudent zyzStudent) {
        ZyzStudent zyzStudent1 = zyzStudentMapper.selectByUsername(zyzStudent.getUsername());
        if (ObjectUtil.isNotNull(zyzStudent1)){
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        String password = zyzStudent.getPassword();
        String md5 = SecureUtil.md5(password);
        zyzStudent.setPassword(md5);
        zyzStudentMapper.updateById(zyzStudent);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public ZyzStudent selectById(Integer id) {
        return zyzStudentMapper.selectById(id);
    }

    /**
     * 查询所有
     * @param zyzStudent
     * @return
     */
    public List<ZyzStudent> selectAll(ZyzStudent zyzStudent) {
        return zyzStudentMapper.selectAll(zyzStudent);
    }

    /**
     * 分页查询
     * @param zyzStudent
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<ZyzStudent> selectPage(ZyzStudent zyzStudent, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ZyzStudent> list = zyzStudentMapper.selectAll(zyzStudent);
        return PageInfo.of(list);
    }

    /**
     * 登录
     * @param account
     * @return
     */
    public Account login(Account account) {
        Account dbZyzStudent = zyzStudentMapper.selectByUsername2(account.getUsername());
        if (!ObjectUtil.isNotNull(dbZyzStudent)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        String password = account.getPassword();
        String md5 = SecureUtil.md5(password);
        if (!dbZyzStudent.getPassword().equals(md5)){
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        //生成token
        String tokenData =  dbZyzStudent.getId() + "-" + RoleEnum.STUDENT.name();
        String token = TokenUtils.createToken(tokenData, dbZyzStudent.getPassword());
        dbZyzStudent.setToken(token);
        return dbZyzStudent;
    }

    /**
     * 查询有多少志愿者
     * @return
     */
    public Integer selectCount() {
        return zyzStudentMapper.selectCount();
    }

    public Account selectById2(Integer valueOf) {
        return zyzStudentMapper.selectById2(valueOf);
    }
}
